package com.golxen.framework.activity;


import com.golxen.framework.GameErrorCode;
import com.golxen.framework.activity.dao.UserActivityBussiness;
import com.golxen.framework.common.BuiltinPlugin;
import com.golxen.framework.event.condition.BaseActivityCondition;
import com.golxen.framework.activity.dao.entity.ActivityInfo;
import com.golxen.framework.event.condition.ConditionPlugin;
import com.golxen.framework.event.condition.dao.entity.ActivityConditionInfo;
import com.golxen.framework.event.condition.dao.entity.UserActivityConditionData;
import com.golxen.framework.common.server.game.GameServer;
import com.golxen.framework.module.AbstractModule;
import com.golxen.framework.user.player.GamePlayer;
import com.golxen.utils.TimeUtils;
import com.mysql.cj.util.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

public class ActivityModule extends AbstractModule {

    private Map<Integer, BaseActivityData> baseActivityDataMap;
    private Map<Integer, List<UserActivityConditionData>> userActivityConditionDataMap;


    public ActivityModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadDB() {
        List<Integer> activityIdList = new ArrayList<>(ActivityMgr.getShowTimeActivityInfoMap().keySet());
        baseActivityDataMap = new ConcurrentHashMap<>();
        userActivityConditionDataMap = UserActivityBussiness.getUserActivityConditionDataMap(player.getUserId(),
                activityIdList);
        return true;
    }

    @Override
    public boolean afterLoadDB() {
        // 填充baseActivityDataMap
        for (Map.Entry<Integer, List<UserActivityConditionData>> dataEntry : userActivityConditionDataMap.entrySet()) {
            addActivityData(ActivityMgr.getActivityInfo(dataEntry.getKey()), dataEntry.getValue(), false);
        }
        // 检测新活动
        checkNewActivityData();
        // 重置检测
        resetDay();
        userActivityConditionDataMap = null;
        return false;
    }

    @Override
    public boolean saveDB() {
        if (null != baseActivityDataMap) {

            List<UserActivityConditionData> insertList = new ArrayList<>();
            List<UserActivityConditionData> updateList = new ArrayList<>();

//            List<BaseActivityData> tempActivityList = new ArrayList<>(baseActivityDataMap.values());
            for (BaseActivityData baseActivityData : baseActivityDataMap.values()) {

                for (UserActivityConditionData userActivityConditionData : baseActivityData
                        .getUserActivityConditionDataList()) {
                    if (userActivityConditionData.isInsertOption()) {
                        // 改为批量存
                        // UserActivityBussiness.addUserActivityConditionData(userActivityConditionData);
                        insertList.add(userActivityConditionData);
                    } else if (userActivityConditionData.isUpdateOption()) {
                        // UserActivityBussiness.updateUserActivityConditionData(userActivityConditionData);
                        updateList.add(userActivityConditionData);
                    }
                }
            }

            if (!insertList.isEmpty()) {
                UserActivityBussiness.batchAddUserActivityConditionData(insertList);
            }

            if (!updateList.isEmpty()) {
                UserActivityBussiness.batchUpdateUserActivityConditionData(updateList);
            }

        }
        return true;
    }

    @Override
    public void loginSendMsg() {
        // 检测新活动
        checkNewActivityData();
        syncActivity();
    }

    public void syncActivity() {

    }



    public void resetDay() {

    }

    public void checkNewActivityData() {
        synchronized (this) {
            if (!ActivityMgr.getShowTimeActivityInfoMap().isEmpty()) {
                // 检查所有活动
                for (ActivityInfo activityInfo : ActivityMgr.getShowTimeActivityInfoMap().values()) {
                    BaseActivityData baseActivityData = baseActivityDataMap.get(activityInfo.getActivityId());
                    if (baseActivityData == null) {
                        addActivityData(activityInfo, new ArrayList<>(), true);
                    } else {
                        addActivityData(activityInfo, baseActivityData.getUserActivityConditionDataList(), false);
                    }
                }

            }
        }
    }

    private void addActivityData(ActivityInfo activityInfo,
            List<UserActivityConditionData> userActivityConditionDataList,boolean isNew) {
        synchronized (this) {
            if (null == activityInfo) {
                return;
            }
            //此活动id的所有条件
            List<ActivityConditionInfo> activityConditionInfoList = ActivityMgr.getActivityConditionInfoMap().get(activityInfo.getActivityId());


            if (null == activityConditionInfoList) {
                return;
            }

            List<BaseActivityCondition> baseActivityConditionList;
            //开始装入baseActivityDataMap
            BaseActivityData baseActivityData = baseActivityDataMap.get(activityInfo.getActivityId());
            if (baseActivityData == null) {
                baseActivityConditionList = new ArrayList<>();
                //第一次构造函数先装入player和activityInfo和userActivityConditionDataList
                baseActivityData = new BaseActivityData(player, activityInfo, baseActivityConditionList,
                        userActivityConditionDataList);
                //遍历用户当前所有的条件然后创建监听器
                for (UserActivityConditionData data : userActivityConditionDataList) {
                    int conditionId = data.getConditionId();
                    ActivityConditionInfo conditionInfo = ActivityMgr
                            .getActivityConditionInfo(activityInfo.getActivityId(), conditionId);
                    if (conditionInfo != null) {
                        BaseActivityCondition baseActivityCondition = (BaseActivityCondition) ConditionPlugin
                                .createActivityCondition(player,conditionInfo,baseActivityData.getUserActivityConditionData(conditionId));
                        baseActivityConditionList.add(baseActivityCondition);
                    }
                }
            } else {
                baseActivityConditionList = baseActivityData.getBaseActivityConditionList();
            }

            // 获取玩家参与的活动的最高值
            Map<Integer, BigInteger> maxValueMap = baseActivityData.getMaxUserActivityConditionData();

            // 遍历检查该活动id下所有活动condition，给这个用户添加缺的活动条件进度
            for (ActivityConditionInfo activityConditionInfo : activityConditionInfoList) {
                if (activityConditionInfo.getType() > 0) {
                    //判断这个类型的BaseActivityCondition是不是已经装入baseActivityData
                    if (baseActivityData.isContinueAddCondition(activityConditionInfo.getType())) {
                    	// 判断这个条件id的BaseActivityCondition是不是已经装入baseActivityData
                    	if (isNew || !baseActivityData.isContainCondition(activityConditionInfo.getConditionId())) {
                            UserActivityConditionData userConditionData = ActivityMgr
                                    .initUserActivityConditionData(player, activityConditionInfo);
                            BigInteger maxValue = maxValueMap.get(activityConditionInfo.getType());
                            if (null == maxValue) {
                                maxValue = BigInteger.ZERO;
                            }
                            userConditionData.setValue(maxValue);
                            baseActivityData.getUserActivityConditionDataList().add(userConditionData);
	                        // 条件逻辑类（监听器）
                            BaseActivityCondition baseActivityCondition = (BaseActivityCondition) ConditionPlugin
                                    .createActivityCondition(player,
                                            activityConditionInfo,
                                            userConditionData);
	                        if (null != baseActivityCondition) {
                                // 新的条件监听器
                                baseActivityConditionList.add(baseActivityCondition);

                            }
                        }
                    }
                }
            }
            //注册监听器
            baseActivityData.addToPlayer();
            baseActivityDataMap.put(activityInfo.getActivityId(), baseActivityData);
        }
    }





    public BaseActivityData getBaseActivityData(int activityId) {
        return baseActivityDataMap.get(activityId);
    }





}
